package day17;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class callablePractice {
    public static void main(String[] args) {
        Callable<Long> cPrc = new callablePrc();
        FutureTask<Long> future1 = new FutureTask(cPrc);
        FutureTask<Long> future2 = new FutureTask(cPrc);
        FutureTask<Long> future3 = new FutureTask(cPrc);
        Thread call1 = new Thread(future1, "老李");
        Thread call2 = new Thread(future2, "老张");
        Thread call3 = new Thread(future3, "老赵");
        call1.start();
        call2.start();
        call3.start();

        try {
            System.out.println("任务耗时：" + (call1.getName()+future1.get() / 1000000) + "毫秒");
            System.out.println("任务耗时：" + (call2.getName()+future2.get() / 1000000) + "毫秒");
            System.out.println("任务耗时：" + (call3.getName()+future3.get() / 1000000) + "毫秒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


    }
}
class callablePrc implements Callable<Long>{
    private int tickets=10000;
    public Long call() throws Exception {
        Long begin=System.nanoTime();
        Object oo=new Object();
        while (tickets>0){
            synchronized (oo){
                System.out.println(Thread.currentThread().getName()+"卖出了第"+tickets+"张票");
                tickets--;
            }

        }
        Long end=System.nanoTime();
        return (end-begin);
    }
}